package com.jsaas.utils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import com.jfinal.kit.StrKit;



/**   
* @Title: MyUtils.java 
* @Package com.xhc.utils 
* @Description: TODO(用一句话描述该文件做什么) 
* @author tuozq 
* @date 2017年10月26日 上午9:00:39 
* @version V1.0   
*/
public class MyUtils {
	
	public static String newId(){
		return UUID.randomUUID().toString().replace("-", "");
	}
	
	/**
	 * 是否ajax请求
	 * @param request
	 * @return
	 */
	public static boolean isAjax(HttpServletRequest request){
		String header = request.getHeader("X-Requested-With");
		return "XMLHttpRequest".equalsIgnoreCase(header);
	}
	
	public static int getIntValue(Object obj) {
		if (obj instanceof String)
			return getIntValue((String) obj);
		if (obj instanceof Integer)
			return getIntValue((Integer) obj);
		if (obj instanceof BigDecimal)
			return getIntValue((BigDecimal) obj);
		if (obj instanceof Double)
			return getIntValue((Double) obj);
		if (obj instanceof Long)
			return getIntValue((Long) obj);
		return 0;
	}

	public static int getIntValue(String obj) {
		return getInt(obj).intValue();
	}

	public static int getIntValue(Integer obj) {
		return obj.intValue();
	}

	public static int getIntValue(BigDecimal obj) {
		return obj.intValue();
	}

	public static int getIntValue(Double obj) {
		return obj.intValue();
	}

	public static int getIntValue(Long obj) {
		return obj.intValue();
	}

	public static int getIntValue(int value) {
		return value;
	}

	public static int getIntValue(double value) {
		return (int) value;
	}

	public static boolean getBooleanValue(Object obj) {
		if (obj instanceof Boolean) {
			return getBooleanValue((Boolean) obj);
		}
		return false;
	}

	public static boolean getBooleanValue(Boolean obj) {
		return obj.booleanValue();
	}

	public static Integer getInt(Object obj) {
		if (obj == null)
			return new Integer(0);
		if (obj instanceof String)
			return getInt((String) obj);
		if (obj instanceof Integer)
			return ((Integer) obj);
		if (obj instanceof BigDecimal)
			return getInt((BigDecimal) obj);
		if (obj instanceof Double)
			return getInt((Double) obj);
		if (obj instanceof Long) {
			return getInt((Long) obj);
		}
		return new Integer(0);
	}

	public static Integer getInt(String value) {
		if (value == null) {
			return new Integer(0);
		}
		return Integer.valueOf(value);
	}

	public static Integer getInt(Long value) {
		if (value == null) {
			return new Integer(0);
		}
		return new Integer(value.intValue());
	}

	public static Integer getInt(double value) {
		return new Integer(new Double(value).intValue());
	}

	public static Integer getInt(int value) {
		return new Integer(value);
	}

	public static Integer getInt(BigDecimal value) {
		if (value == null) {
			return new Integer(0);
		}
		return new Integer(value.intValue());
	}

	public static Integer getInt(Double value) {
		if (value == null) {
			return new Integer(0);
		}
		return new Integer(value.intValue());
	}

	public static Boolean getBoolean(Object obj) {
		if (obj instanceof Boolean) {
			return ((Boolean) obj);
		}
		return null;
	}

	public static Boolean getBoolean(boolean obj) {
		return new Boolean(obj);
	}

	public static int abs(int value) {
		if (value >= 0) {
			return value;
		}
		return (-1 * value);
	}

	public static double abs(double value) {
		if (value >= 0.0D) {
			return value;
		}
		return (-1.0D * value);
	}

	public static int abs(Integer value) {
		if (value == null)
			return 0;
		if (value.intValue() >= 0) {
			return value.intValue();
		}
		return (-1 * value.intValue());
	}

	public static double abs(Double value) {
		if (value == null)
			return 0.0D;
		if (value.doubleValue() >= 0.0D) {
			return value.doubleValue();
		}
		return (-1.0D * value.doubleValue());
	}

	public static double abs(BigDecimal value) {
		if (value == null)
			return 0.0D;
		if (value.doubleValue() >= 0.0D) {
			return value.doubleValue();
		}
		return (-1.0D * value.doubleValue());
	}

	public static long abs(Long value) {
		if (value == null)
			return 0L;
		if (value.doubleValue() >= 0.0D) {
			return value.longValue();
		}
		return (-1L * value.longValue());
	}

	public static double abs(Object value) {
		if (value == null)
			return 0.0D;
		if (value instanceof Integer)
			return new Double(abs((Integer) value)).doubleValue();
		if (value instanceof Double)
			return abs((Double) value);
		if (value instanceof BigDecimal)
			return abs((BigDecimal) value);
		if (value instanceof Long) {
			return new Double(abs((Long) value)).doubleValue();
		}
		return 0.0D;
	}

	public static int len(Object value) {
		return len((String) value);
	}

	public static int len(String value) {
		return value.length();
	}

	public static String left(Object value, int length) {
		return left((String) value, length);
	}

	public static String left(String value, int length) {
		if ((length > 0) && (length <= value.length()))
			return value.substring(0, length);
		if (length > 0) {
			return value;
		}
		return "";
	}

	public static String right(Object value, int length) {
		return right((String) value, length);
	}

	public static String right(String value, int length) {
		if ((length > 0) && (length <= value.length()))
			return value.substring(value.length() - length);
		if (length > 0) {
			return value;
		}
		return "";
	}

	public static String upper(Object value) {
		return upper((String) value);
	}

	public static String upper(String value) {
		return value.toUpperCase();
	}

	public static String lower(Object value) {
		return lower((String) value);
	}

	public static String lower(String value) {
		return value.toLowerCase();
	}

	public static String trim(Object value) {
		return trim((String) value);
	}

	public static String trim(String value) {
		return value.trim();
	}

	public static double getBigDecimalValue(Object obj) {
		if (obj instanceof String)
			return getBigDecimalValue((String) obj);
		if (obj instanceof BigDecimal)
			return getBigDecimalValue((BigDecimal) obj);
		if (obj instanceof Double)
			return getBigDecimalValue((Double) obj);
		if (obj instanceof Integer)
			return getBigDecimalValue((Integer) obj);
		return 0.0D;
	}

	public static double getBigDecimalValue(String obj) {
		return getBigDecimal(obj).doubleValue();
	}

	public static double getBigDecimalValue(Double obj) {
		return obj.doubleValue();
	}

	public static double getBigDecimalValue(BigDecimal obj) {
		return obj.doubleValue();
	}

	public static double getBigDecimalValue(Integer obj) {
		return getBigDecimal(obj).doubleValue();
	}

	public static double getBigDecimalValue(int value) {
		return value;
	}

	public static double getBigDecimalValue(double value) {
		return value;
	}

	public static BigDecimal getBigDecimal(Object obj) {
		if (obj == null)
			return new BigDecimal(0.0D);
		if (obj instanceof String)
			return getBigDecimal((String) obj);
		if (obj instanceof BigDecimal)
			return ((BigDecimal) obj);
		if (obj instanceof Double)
			return getBigDecimal((Double) obj);
		if (obj instanceof Integer) {
			return getBigDecimal((Integer) obj);
		}
		return new BigDecimal(0.0D);
	}

	public static BigDecimal getBigDecimal(Double value) {
		if (value == null) {
			return new BigDecimal(0.0D);
		}
		return new BigDecimal(value.doubleValue());
	}

	public static BigDecimal getBigDecimal(Integer value) {
		if (value == null) {
			return new BigDecimal(0.0D);
		}
		return new BigDecimal(value.doubleValue());
	}

	public static BigDecimal getBigDecimal(String value) {
		if (value == null) {
			return new BigDecimal(0.0D);
		}
		return new BigDecimal(value);
	}

	public static BigDecimal getBigDecimal(double value) {
		return new BigDecimal(value);
	}

	public static BigDecimal getBigDecimal(int value) {
		return BigDecimal.valueOf(value);
	}

	public static double getDouble(Object obj) {
		if(obj instanceof String){
			return getDouble((String)obj);
		}else if(obj instanceof BigDecimal){
			return getDouble((BigDecimal)obj);
		}else if(obj instanceof Double){
			return getDouble((Double)obj);
		}else if(obj instanceof Number){
			return getDouble((Number)obj);
		}
		return 0.0D;
	}
	
	public static double getDouble(Number obj) {
		if(null == obj){
			return 0.0D;
		}else{
			return obj.doubleValue();
		}
		
	}

	public static double getDouble(String text) {
		if (text == null || text.trim().length() == 0) {
			return 0.0D;
		}
		try {
			double value = Double.parseDouble(text);
			return value;
		} catch (Exception e) {
		}
		return 0.0D;
	}

	public static double getDoubleValue(Double obj) {
		return obj.doubleValue();
	}

	public static Double getDouble(double value) {
		return new Double(value);
	}

	public static String getString(int value) {
		return String.valueOf(value);
	}

	public static String getString(double value) {
		return String.valueOf(value);
	}

	public static String getString(Object value) {
		if (value == null) {
			return "";
		}
		return String.valueOf(value);
	}
	
	
	public static boolean isNull(Object value) {
		if (value instanceof String)
			return isNull((String) value);
		if (value instanceof Integer)
			return isNull((Integer) value);
		if (value instanceof Float)
			return isNull((Float) value);
		if (value instanceof Double)
			return isNull((Double) value);
		if (value instanceof BigDecimal)
			return isNull((BigDecimal) value);
		if (value instanceof Date) {
			return isNull((Date) value);
		}
		return (value == null);
	}

	public static boolean isNull(String value) {
		return StrKit.isBlank(value);
	}

	public static boolean isNull(int value) {
		return ((value == 0) || (value == -1));
	}

	public static boolean isNull(float value) {
		return (value == 0.0F);
	}

	public static boolean isNull(double value) {
		return (value == 0.0D);
	}

	public static boolean isNull(Integer value) {
		return ((value == null) || (value.intValue() == 0));
	}

	public static boolean isNull(Float value) {
		return ((value == null) || (isNull(value.floatValue())));
	}

	public static boolean isNull(Double value) {
		return ((value == null) || (isNull(value.doubleValue())));
	}

	public static boolean isNull(BigDecimal value) {
		return ((value == null) || (isNull(value.doubleValue())));
	}

	public static boolean isNull(Date value) {
		return (value == null);
	}

	public static boolean isNotNull(Object value) {
		if (value instanceof Integer)
			return isNotNull((Integer) value);
		if (value instanceof Float)
			return isNotNull((Float) value);
		if (value instanceof Double)
			return isNotNull((Double) value);
		if (value instanceof BigDecimal)
			return isNotNull((BigDecimal) value);
		if (value instanceof Date) {
			return isNotNull((Date) value);
		}
		return (value != null);
	}

	public static boolean isNotNull(String value) {
		return (!(isNull(value)));
	}

	public static boolean isNotNull(int value) {
		return (!(isNull(value)));
	}

	public static boolean isNotNull(float value) {
		return (!(isNull(value)));
	}

	public static boolean isNotNull(double value) {
		return (!(isNull(value)));
	}

	public static boolean isNotNull(Integer value) {
		return (!(isNull(value)));
	}

	public static boolean isNotNull(Float value) {
		return (!(isNull(value)));
	}

	public static boolean isNotNull(Double value) {
		return (!(isNull(value)));
	}

	public static boolean isNotNull(BigDecimal value) {
		return (!(isNull(value)));
	}

	public static boolean isNotNull(Date value) {
		return (!(isNull(value)));
	}

	public static boolean isEmpty(String value) {
		return value == null || value.trim().length() == 0;
	}

	public static boolean isBlank(String value) {
		return value == null || value.trim().length() == 0;
	}

	public static boolean isNotEmpty(String value) {
		return value != null && value.trim().length() > 0;
	}

	public static boolean isNotBlank(String value) {
		return value != null && value.trim().length() > 0;
	}
}
